﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODS.Config;
using ODS.Model;
using ODS.Utils;
using System.IO;
using System.Xml.Linq;
using System.Windows;

namespace ODS.WebService.DeviceMonitor
{
    public class RemoveDeviceMonitorSevice
    {
        static bool IsAuthenticated = true;

        public static async Task<InfoResult> RemoveDeviceMonitor(String imei, String monitorType)
        {
            String url = Constants.SERVER_URL + "/devices/" + imei + "/monitors/" + monitorType; ;

            InfoResult result;
            try
            {
                Stream xmlStream = await RESTfulService.HTTPDeleteRequest(url);

                String resultString = ParseXML(xmlStream);

                #region check authenticate
                if (!IsAuthenticated)
                {
                    InfoResult result_login = await Login_Logout.LoginService.loginService(App.userLogin.UserLoginName, App.userLogin.UserPassword);
                    if (result_login.IsSuccess)
                    {
                        if ((String)result_login.ReturnedObject == Constants.SUCCESS)
                        {
                            IsAuthenticated = true;

                            return await RemoveDeviceMonitor(imei,monitorType);
                        }
                        else
                        {
                            MessageBox.Show((String)result_login.ReturnedObject);
                            App.BackToLoginPage();

                        }
                    }
                    else
                    {
                        MessageBox.Show(result_login.Error);
                        App.BackToLoginPage();

                    }


                }
                #endregion

                result = new InfoResult(null, resultString);
            }
            catch (Exception ex)
            {
                result = new InfoResult(ex.Message, null);
            }
            return result;
        }

        private static String ParseXML(Stream xmlStream)
        {
            XDocument doc = XDocument.Load(xmlStream);

            var Authenticate = (from authen in doc.Descendants("unauthorized")
                                select new
                                {

                                }).FirstOrDefault();
            if (Authenticate != null)
            {
                IsAuthenticated = false;
                return null;
            }

            var Nack = (from nack in doc.Descendants("nack")
                        select new
                        {
                            Message = nack.Element("message").Value,
                        }).FirstOrDefault();

            if (Nack != null)
            {
                return Nack.Message;
            }
            else
            {
                return Constants.SUCCESS;
            }

        }
    }
}
